package com.gouaixin.bee.beeframework2.tool;

/**
 * Created by Administrator on 2015/4/21.
 */

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间处理工具类
 * 1.时间搓用time表示
 * 2.年月日制标准时间用date表示
 */
public class TimeUtil {

    //获得当前时间搓
    public static long getCurrentTime() {
        return new Date().getTime();
    }

    //获得当前的年
    public static long getCurrentYear() {
        Calendar cld = Calendar.getInstance();
        int currentYear = cld.get(Calendar.YEAR);
        int currentMonth = cld.get(Calendar.MONTH) + 1;
        long currentDay = cld.get(Calendar.DAY_OF_MONTH);
        return currentYear;
    }

    //获得当前的月
    public static long getCurrentMouth() {
        Calendar cld = Calendar.getInstance();
        int currentYear = cld.get(Calendar.YEAR);
        int currentMonth = cld.get(Calendar.MONTH) + 1;
        long currentDay = cld.get(Calendar.DAY_OF_MONTH);
        return currentMonth;
    }
    //获得当前的日
    public static long getCurrentDay() {
        Calendar cld = Calendar.getInstance();
        int currentYear = cld.get(Calendar.YEAR);
        int currentMonth = cld.get(Calendar.MONTH) + 1;
        long currentDay = cld.get(Calendar.DAY_OF_MONTH) ;
        return currentDay;
    }

    //获得当前的时
    public static long getCurrentHour() {
        Calendar cld = Calendar.getInstance();
        long currentHour = cld.get(Calendar.HOUR_OF_DAY) ;
        return currentHour;
    }

    //获得当前的分
    public static long getCurrentMin() {
        Calendar cld = Calendar.getInstance();
        long currentMin = cld.get(Calendar.MINUTE) ;
        return currentMin;
    }

    //获得当前的秒
    public static long getCurrentSecond() {
        Calendar cld = Calendar.getInstance();
        long currentSecond = cld.get(Calendar.SECOND) ;
        return currentSecond;
    }

    //获得当前的毫秒
    public static long getCurrentMs() {
        Calendar cld = Calendar.getInstance();
        long currentMs= cld.get(Calendar.MILLISECOND) ;
        return currentMs;
    }


    //将时间搓转为年月日
    public static String timeToDate(long time) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
    }

    //获得指定年月日的时间搓
    public static long dateToTime(long year, long mouth, long day) {
        long result = 0;
        String tomorrowStr = year + "-" + mouth + "-"
                + day + " " + "00:00:00.000";
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        try {
            result = dfs.parse(tomorrowStr).getTime();
        } catch (ParseException e) {
            Log.e("time", "获得指定年月日的时间搓:失败=" + e.getMessage());
        }
        return result;
    }

    //获得指定年月日时分秒的时间搓
    public static long dateToTime(long year, long mouth, long day,long hour,long min,long second) {
        long result = 0;
        String tomorrowStr = year + "-" + mouth + "-"
                + day + " " +hour+":"+min+":"+second+".000";
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        try {
            result = dfs.parse(tomorrowStr).getTime();
        } catch (ParseException e) {
            Log.e("time", "获得指定年月日的时间搓:失败=" + e.getMessage());
        }
        return result;
    }

    //根据时间搓获得日
    public static long getDay(long time) {
        long day = time / (24 * 60 * 60 * 1000);
        long hour = (time / (60 * 60 * 1000) - day * 24);
        long min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long second = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (time - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - second * 1000);
        return day;
    }

    //根据时间搓获得时
    public static long getHour(long time) {
        long day = time / (24 * 60 * 60 * 1000);
        long hour = (time / (60 * 60 * 1000) - day * 24);
        long min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long second = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (time - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - second * 1000);
        return hour;
    }

    //根据时间搓获得分
    public static long getMin(long time) {
        long day = time / (24 * 60 * 60 * 1000);
        long hour = (time / (60 * 60 * 1000) - day * 24);
        long min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long second = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (time - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - second * 1000);
        return min;
    }

    //根据时间搓获得秒
    public static long getSecond(long time) {
        long day = time / (24 * 60 * 60 * 1000);
        long hour = (time / (60 * 60 * 1000) - day * 24);
        long min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long second = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (time - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - second * 1000);
        return second;
    }

    //根据时间搓获得毫秒
    public static long getMs(long time) {
        long day = time / (24 * 60 * 60 * 1000);
        long hour = (time / (60 * 60 * 1000) - day * 24);
        long min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long second = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (time - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - second * 1000);

        return ms;
    }

    //根据当前时间戳和年数，得到截止时间搓
    public static long getEndTimeWithYear(int yearNumber) {
        long currentTime=getCurrentTime();
        long endTime=0;
        long currentYear=getCurrentYear();
        long currentMouth=getCurrentMouth();
        long currentDay=getCurrentDay();
        endTime=dateToTime(currentYear+yearNumber,currentMouth,currentDay);
        return endTime;
    }

    //根据当前时间戳和月数，得到截止时间搓
    public static long getEndTimeWithMouth(int mouthNumber) {
        long currentTime=getCurrentTime();

        mLog.e("当前时间戳:" + currentTime);
        long endTime=0;
        long currentYear=getCurrentYear();
        long currentMouth=getCurrentMouth();
        long currentDay=getCurrentDay();
        long currentHour=getCurrentHour();
        long currentMin=getCurrentMin();
        long currentSecond=getCurrentSecond();

        endTime=dateToTime(currentYear,currentMouth+mouthNumber,currentDay,currentHour,currentMin,currentSecond);

        mLog.e("截止时间戳:" + endTime);
        return endTime;
    }

    //根据当前时间戳和天数，得到截止时间搓
    public static long getEndTimeWithDay(int dayNumber) {
        long currentTime=getCurrentTime();
        long endTime=0;
        long currentYear=getCurrentYear();
        long currentMouth=getCurrentMouth();
        long currentDay=getCurrentDay();
        endTime=dateToTime(currentYear,currentMouth,currentDay+dayNumber);
        return endTime;
    }



}
